package com.smart.vista.dialog;

import com.smart.entity.ClienteLocal;
import com.smart.entity.GuiasSoporte;
import com.smart.main.Main;
import com.smart.main.Transaccion;
import com.smart.vista.panel.GuiasSoporteVista;
import java.awt.event.KeyEvent;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.swing.JOptionPane;

public class GuiaSoporteNuevo extends javax.swing.JDialog {

    private ClienteLocal objClienteLocalActivo = null;
    private final String tipoServicio = "Soporte Tecnico";

    public GuiaSoporteNuevo(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(null);
        cboFechaAtencion.setDate(new java.util.Date());
        txtNroGuia.grabFocus();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        panel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtNroGuia = new com.smart.comp.TextoNumeros();
        jLabel7 = new javax.swing.JLabel();
        cboFechaAtencion = new com.toedter.calendar.JDateChooser();
        panel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        txtClienteLocal = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtCargo = new javax.swing.JTextField();
        txtAtencion = new javax.swing.JTextField();
        panel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtObservacion = new javax.swing.JTextArea();
        pnlBotones = new javax.swing.JPanel();
        btnGuardar = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Nueva Guia de Soporte Tecnico");
        setResizable(false);

        panel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        panel1.setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("Fecha Atencion: ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        panel1.add(jLabel1, gridBagConstraints);

        jLabel2.setText("Guia por: ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        panel1.add(jLabel2, gridBagConstraints);

        jLabel3.setText("Nro. Guia: ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        panel1.add(jLabel3, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        panel1.add(txtNroGuia, gridBagConstraints);

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel7.setText(" Soporte Tecnico ");
        jLabel7.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        panel1.add(jLabel7, gridBagConstraints);

        cboFechaAtencion.setDateFormatString("dd.MM.yyyy");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        panel1.add(cboFechaAtencion, gridBagConstraints);

        panel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        panel2.setLayout(new java.awt.GridBagLayout());

        jLabel4.setText("Compañia:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        panel2.add(jLabel4, gridBagConstraints);

        txtClienteLocal.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtClienteLocalKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        panel2.add(txtClienteLocal, gridBagConstraints);

        jLabel5.setText("Cargo:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        panel2.add(jLabel5, gridBagConstraints);

        jLabel6.setText("Atencion:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        panel2.add(jLabel6, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.5;
        panel2.add(txtCargo, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 1.0;
        panel2.add(txtAtencion, gridBagConstraints);

        panel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Observaciones"));

        txtObservacion.setColumns(20);
        txtObservacion.setLineWrap(true);
        txtObservacion.setRows(5);
        txtObservacion.setWrapStyleWord(true);
        jScrollPane1.setViewportView(txtObservacion);

        javax.swing.GroupLayout panel3Layout = new javax.swing.GroupLayout(panel3);
        panel3.setLayout(panel3Layout);
        panel3Layout.setHorizontalGroup(
            panel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 625, Short.MAX_VALUE)
        );
        panel3Layout.setVerticalGroup(
            panel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)
        );

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/smart/recursos/disckete.png"))); // NOI18N
        btnGuardar.setText("Guardar");
        btnGuardar.setToolTipText("Guardar");
        btnGuardar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnGuardar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnSalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/smart/recursos/salir.png"))); // NOI18N
        btnSalir.setText("Salir");
        btnSalir.setToolTipText("Salir");
        btnSalir.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSalir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlBotonesLayout = new javax.swing.GroupLayout(pnlBotones);
        pnlBotones.setLayout(pnlBotonesLayout);
        pnlBotonesLayout.setHorizontalGroup(
            pnlBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlBotonesLayout.createSequentialGroup()
                .addContainerGap(429, Short.MAX_VALUE)
                .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        pnlBotonesLayout.setVerticalGroup(
            pnlBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBotonesLayout.createSequentialGroup()
                .addGroup(pnlBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSalir)
                    .addComponent(btnGuardar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panel1, javax.swing.GroupLayout.DEFAULT_SIZE, 637, Short.MAX_VALUE)
            .addComponent(panel2, javax.swing.GroupLayout.DEFAULT_SIZE, 637, Short.MAX_VALUE)
            .addComponent(pnlBotones, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(panel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panel1, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlBotones, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        this.setVisible(false);
}//GEN-LAST:event_btnSalirActionPerformed

    private void txtClienteLocalKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtClienteLocalKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            ListaClienteLocal jdBuscarCliente = new ListaClienteLocal(Main.PADRE, true);
            jdBuscarCliente.filtrar(txtClienteLocal.getText());
            jdBuscarCliente.setVisible(true);
            if (jdBuscarCliente.getObjClienteLocal() != null) {
                objClienteLocalActivo = jdBuscarCliente.getObjClienteLocal();
                txtClienteLocal.setText(objClienteLocalActivo.getNombreLocal());
                txtAtencion.grabFocus();
            }
            jdBuscarCliente.dispose();
        }
    }//GEN-LAST:event_txtClienteLocalKeyPressed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        int rpta1 = JOptionPane.showConfirmDialog(this, "Seguro de Guardar la Guia", this.getTitle(), JOptionPane.YES_NO_OPTION);
        if (rpta1 == JOptionPane.YES_OPTION) {
            if (validaCampos()) {
                java.util.Date fecha = cboFechaAtencion.getDate();
                String nroGuia = txtNroGuia.getText();
                String atencion = txtAtencion.getText();
                String cargo = txtCargo.getText();
                String servicio = tipoServicio;
                String observacion = txtObservacion.getText();
                GuiasSoporte guiaSoporte = new GuiasSoporte();
                guiaSoporte.setFecha(fecha);
                guiaSoporte.setServicio(servicio);
                guiaSoporte.setNroguia(nroGuia);
                guiaSoporte.setClienteLocal(objClienteLocalActivo);
                guiaSoporte.setAtencion(atencion);
                guiaSoporte.setCargo(cargo);
                guiaSoporte.setObservacion(observacion);
                guiaSoporte.setUsuarios(Main.USUARIO_LOGEADO);
                Transaccion.getSession().save(guiaSoporte);
                Transaccion.closeSession();
                Object object[] = {getFechaNormal(fecha), nroGuia, objClienteLocalActivo.getNombreLocal(), atencion, cargo, Main.USUARIO_LOGEADO.getNombres()};
                GuiasSoporteVista.getModeloTblGuiaSoporte().addRow(object);
                int rpta2 = JOptionPane.showConfirmDialog(this, "Desea registrar otra Guia?", this.getTitle(), JOptionPane.YES_NO_OPTION);
                if (rpta2 == JOptionPane.YES_OPTION) {
                    limpiar();
                } else {
                    this.setVisible(false);
                }
            }
        }
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void limpiar() {
        //cboFechaAtencion.setDate(new Date());
        txtNroGuia.setText("");
        objClienteLocalActivo = null;
        txtClienteLocal.setText("");
        txtAtencion.setText("");
        txtCargo.setText("");
        txtObservacion.setText("");
        txtNroGuia.grabFocus();
    }

    private String getFechaNormal(java.util.Date fecha) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        return sdf.format(fecha);
    }

    private boolean validaCampos() {
        boolean validado = true;
        String HQL = "from GuiasSoporte gs where gs.nroguia='" + txtNroGuia.getText() + "'";
        List<GuiasSoporte> lstGuiaBuscada = Transaccion.executeHQLQuery(HQL);
        String nroGuia = "";
        for (GuiasSoporte guiasSoporte : lstGuiaBuscada) {
            nroGuia = guiasSoporte.getNroguia();
        }
        if (tipoServicio.equals("")) {
            //JOptionPane.showMessageDialog(this, "");
        } else if (txtNroGuia.getText().equals("")) {
            validado = false;
            JOptionPane.showMessageDialog(this, "Ingrese el Numero de Guia");
            txtNroGuia.grabFocus();
        } else if (!nroGuia.equals("")) {
            validado = false;
            JOptionPane.showMessageDialog(this, "La Guia " + txtNroGuia.getText() + " existe");
            txtNroGuia.setText("");
            txtNroGuia.grabFocus();
        } else if (txtNroGuia.getText().length() > 6) {
            validado = false;
            JOptionPane.showMessageDialog(this, "El Numero de la Guia debe contener 6 caracteres");
            txtNroGuia.setText("");
            txtNroGuia.grabFocus();
        } else if (objClienteLocalActivo == null) {
            validado = false;
            JOptionPane.showMessageDialog(this, "Elija un Cliente");
            txtClienteLocal.grabFocus();
        } else if (txtAtencion.getText().equals("")) {
            validado = false;
            JOptionPane.showMessageDialog(this, "Ingrese Atencion");
            txtAtencion.grabFocus();
        } else if (txtObservacion.getText().equals("")) {
            validado = false;
            JOptionPane.showMessageDialog(this, "Ingrese la observacion");
            txtObservacion.grabFocus();
        }
        return validado;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnSalir;
    private com.toedter.calendar.JDateChooser cboFechaAtencion;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel panel1;
    private javax.swing.JPanel panel2;
    private javax.swing.JPanel panel3;
    private javax.swing.JPanel pnlBotones;
    private javax.swing.JTextField txtAtencion;
    private javax.swing.JTextField txtCargo;
    private javax.swing.JTextField txtClienteLocal;
    private com.smart.comp.TextoNumeros txtNroGuia;
    private javax.swing.JTextArea txtObservacion;
    // End of variables declaration//GEN-END:variables
}
